package com.ning.parking.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingUserInfo;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingUserInfoDto;
import com.ning.parking.service.ParkingUserInfoService;
import com.ning.parking.mapper.ParkingUserInfoMapper;
import com.scsonic.common.enums.StatusEnum;
import com.scsonic.common.utils.AssertUtil;
import com.scsonic.framework.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 *
 */
@Service
public class ParkingUserInfoServiceImpl extends ServiceImpl<ParkingUserInfoMapper, ParkingUserInfo> implements ParkingUserInfoService {

    @Override
    public String login(ParkingUserInfo parkingUserInfo) {

        String loginPassword = UUID.nameUUIDFromBytes(parkingUserInfo.getPassword().getBytes()).toString();

        ParkingUserInfo one = this.lambdaQuery()
                .eq(ParkingUserInfo::getAccount, parkingUserInfo.getAccount())
                .one();
        if (one == null) {
            throw new BizException(one.getAccount()+ "账号不存在");
        }

        if (!loginPassword.equals(one.getPassword())) {
            throw new BizException("密码错误");
        }

        AssertUtil.isFalse( one.getStatus() == StatusEnum.NO, "账号已停用！");

        String accessToken = UUID.randomUUID().toString().replaceAll("-", "");

        one.setToken(accessToken);

        one.setUpdateTime(new Date());

        updateById(one);

        GlobalVariableUtils.setUser(accessToken,one);

        return accessToken;
    }

    @Override
    public ParkingUserInfo saveUser(ParkingUserInfo parkingUserInfo) {
        String password = parkingUserInfo.getPassword();
        if (password==null){
            password = "xiaoning";
        }
        parkingUserInfo.setPassword(UUID.nameUUIDFromBytes(password.getBytes()).toString());
        parkingUserInfo.setStatus(StatusEnum.YES);
        if (parkingUserInfo.getUserId() == null) {
            parkingUserInfo.setCreateTime(new Date());
            parkingUserInfo.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        }
        parkingUserInfo.setUpdateTime(new Date());
        parkingUserInfo.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        saveOrUpdate(parkingUserInfo);
        return parkingUserInfo;
    }

    @Override
    public Pagination<ParkingUserInfo> userPage(ParkingUserInfoDto parkingUserInfo) {
        Page<ParkingUserInfo> page = lambdaQuery()
                .like(ObjectUtil.isNotEmpty(parkingUserInfo.getUserName()),ParkingUserInfo::getUserName,parkingUserInfo.getUserName())
                .like(ObjectUtil.isNotEmpty(parkingUserInfo.getAccount()),ParkingUserInfo::getAccount,parkingUserInfo.getAccount())
                .like(ObjectUtil.isNotEmpty(parkingUserInfo.getEmployeeNo()),ParkingUserInfo::getEmployeeNo,parkingUserInfo.getEmployeeNo())
                .like(ObjectUtil.isNotEmpty(parkingUserInfo.getPhone()),ParkingUserInfo::getPhone,parkingUserInfo.getPhone()).orderByDesc(ParkingUserInfo::getUpdateTime).page(parkingUserInfo.toPage());
        return Pagination.of(page);
    }

    @Override
    public void logout(String token) {
        GlobalVariableUtils.setUser(token,null);
    }
}
